package com.kaesar.leetcode.LeetCode_251_300;

public class LeetCode_278 extends VersionControl {
    /**
     * 二分法
     *
     * @param n
     * @return
     */
    public static int firstBadVersion(int n) {
        int low = 1, high = n;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (isBadVersion(mid)) {
                if (mid > low && isBadVersion(mid - 1)) {
                    high = mid - 1;
                } else {
                    return mid;
                }
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(firstBadVersion(2126753390));
    }
}

class VersionControl {
    /**
     * 测试数据，前1702766718个版本都不是错误版本，从第1702766719个版本开始都是错误版本
     */
    public static boolean isBadVersion(int version) {
        if (version < 1702766719) {
            return false;
        } else {
            return true;
        }
    }
}
